<template>
  <div>
    <h1>器械查询</h1>
    <el-card>
      <el-form :inline="true" :model="queryParams">
        <el-form-item label="设备名称">
          <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="deviceList" border style="width: 100%">
        <el-table-column prop="id" label="设备ID" width="80" />
        <el-table-column prop="deviceName" label="设备名称" />
        <el-table-column prop="model" label="型号" />
        <el-table-column prop="specification" label="规格" />
        <el-table-column prop="manufacturer" label="生产厂家" />
        <el-table-column prop="purchaseDate" label="购买日期" />
        <el-table-column prop="warrantyMonths" label="保修期(月)" />
        <el-table-column prop="inventory" label="库存量" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next"
      />
    </el-card>
    <el-dialog v-model="detailDialogVisible" title="设备详情" width="50%">
    <el-descriptions :column="2" border>
      <el-descriptions-item label="设备ID">{{ currentDevice.id }}</el-descriptions-item>
      <el-descriptions-item label="设备名称">{{ currentDevice.deviceName }}</el-descriptions-item>
      <el-descriptions-item label="型号">{{ currentDevice.model }}</el-descriptions-item>
      <el-descriptions-item label="规格">{{ currentDevice.specification }}</el-descriptions-item>
      <el-descriptions-item label="生产厂家">{{ currentDevice.manufacturer }}</el-descriptions-item>
      <el-descriptions-item label="购买日期">{{ currentDevice.purchaseDate }}</el-descriptions-item>
      <el-descriptions-item label="保修期(月)">{{ currentDevice.warrantyMonths }}</el-descriptions-item>
      <el-descriptions-item label="库存量">{{ currentDevice.inventory }}</el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <el-button @click="detailDialogVisible = false">关闭</el-button>
    </template>
  </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { list } from '@/api/device'

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  deviceName: ''
})

const deviceList = ref([])
const total = ref(0)

const getList = () => {
  list(queryParams.value).then(response => {
    deviceList.value = response
    total.value = response.total
  })
}

const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    name: ''
  }
  getList()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

const detailDialogVisible = ref(false)
const currentDevice = ref({})

const handleDetail = (row) => {
  currentDevice.value = { ...row }
  detailDialogVisible.value = true
}


onMounted(() => {
  getList()
})
</script>

<style scoped>
.el-card {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 20px;
  justify-content: center;
}
</style>